package jpa4azure.type;

import java.lang.reflect.Field;

import com.windowsazure.samples.EdmType;
import com.windowsazure.samples.Property;
import com.windowsazure.samples.PropertyCollection;

public class EnumContext extends FieldContext {

	public EnumContext(Object o, Field field) {
		super(o, field);
	}

	public void	getCloudTableColumn(PropertyCollection values) throws Exception {		
		
		if ( value() == null)
			return;
		String val = value().toString();
		values.add(Property.fromRepresentation(
				getFieldname(), 
				EdmType.EdmString, 
				val));
	}
	
	@Override
	public void setProperty(Object o) {
		try {
			//field.setAccessible(true);
			Class cls = field.getType();
			Object enumVal = Enum.valueOf(cls , o.toString());			
			field.set(this.subject, enumVal);
		} catch (Exception e) {
			e.printStackTrace();		
		}
	}	
	
	
}
